package plug.bookmarks;

import java.sql.*;

import com.uujava.minibox.config.Config;

public class UpdateTest {
	public void runInsertDelete() {
		try {
			//String sourceURL = "jdbc:h2:./h2db/minibox";// H2 database
			String sourceURL = "jdbc:h2:"+Config.DATABASE_PATH;// H2 database
			String user = "sa";
			String key = "";
			try {
				Class.forName("org.h2.Driver");// H2 Driver
			} catch (Exception e) {
				e.printStackTrace();
			}
			Connection conn = DriverManager.getConnection(sourceURL, user, key);
			Statement stmt = conn.createStatement();
			stmt.execute("DROP TABLE if EXISTS bookmarks");//创建数据库
			
			//----------------------------------------------------------------------------------

			stmt.execute("CREATE TABLE bookmarks (url varchar(255) NOT NULL PRIMARY KEY,title varchar(255) NOT NULL,icon blob,annotation text ,add_date datetime default NOW(),use_frequency int(11) default 0,subgroup varchar(50) NOT NULL default '默认收藏',PRIMARY KEY  (url)) ");// 创建表
			
			stmt.executeUpdate("INSERT INTO BOOKMARKS(URL,TITLE)  VALUES('http://www.baidu.com/','百度')");//插入
			stmt.executeUpdate("INSERT INTO BOOKMARKS(URL,TITLE)  VALUES('http://www.google.com/','Google');");//插入
			stmt.executeUpdate("INSERT INTO BOOKMARKS(URL,TITLE)  VALUES('http://www.coolqian.com/','苦钱');");//插入
			stmt.executeUpdate("INSERT INTO BOOKMARKS(URL,TITLE)  VALUES('http://www.mxcyk.com/','梦续残源');");//插入
			stmt.executeUpdate("INSERT INTO BOOKMARKS(URL,TITLE)  VALUES('http://www.uujava.com/','悠悠java');");//插入
			stmt.executeUpdate("INSERT INTO BOOKMARKS(URL,TITLE,subgroup )  VALUES('http://www.java.net/','javanet','java');");//插入
			//stmt.executeUpdate("");//插入
			
			
			//----------------------------------------------------------------------------------
//			stmt.execute("CREATE TABLE bookmarks(name VARCHAR(100),sex VARCHAR(10))");//创建表
//			stmt.executeUpdate("INSERT INTO bookmarks VALUES('Steven Stander','male')");//插入
//			stmt.executeUpdate("INSERT INTO bookmarks VALUES('Elizabeth Eames','female')");//插入
//			stmt.executeUpdate("DELETE FROM bookmarks WHERE sex='male'");//删除
//			ResultSet rset = stmt.executeQuery("select name,sex from bookmarks");//查询
			ResultSet rset = stmt.executeQuery("select * from bookmarks");//查询
			while (rset.next()) {
				System.out.println(rset.getString("url") + " "
						+ rset.getString("title"));
			}
			rset.close();
			stmt.close();
			conn.close();
		} catch (SQLException sqle) {
			System.err.println(sqle);
		}
	}

	public static void main(String[] args) {
		new UpdateTest().runInsertDelete();
	}
}